'use client';
import {Button} from "@/components/ui/button";
import {useRouter} from "next/navigation";
import {useEffect, useState} from "react";
import {formatDate} from "@/lib/utils";

const Page = ({params}: { params: { id: string } }) => {
    const [student, setStudent] = useState()

    useEffect(() => {
        (async () => {
            console.log(params.id)
            const res = await fetch(
                `http://localhost:3000/api/student/get/${params.id}`, {
                    method: "GET",
                });
            const json = await res.json();
            console.log(json.student)
            await setStudent(json.student)
        })()
    }, [])

    const route = useRouter()

    if (!student) return null

    return (
        <div className={'flex flex-col items-center gap-4'}>
            <div className={'font-bold text-4xl'}>删除学生</div>
            <div>
                {student.name}
            </div>
            <div>
                {formatDate(student.createdAt)}
            </div>
            <div>
                {formatDate(student.updatedAt)}
            </div>
            <form onSubmit={async (e) => {
                await fetch(`/api/student/delete/${params.id}`, {
                    method: 'POST',
                    body: JSON.stringify({"id": params.id}),
                    headers: {
                        "Content-Type": "application/json"
                    }
                })
                route.back()
            }}>
                <Button type={'submit'} variant={'destructive'}>删除</Button>
            </form>
        </div>
    );
};

export default Page;